<?php
class CustomersController extends AppController {

	var $name = 'Customers';
	var $uses = array('Customer');
	private $group;

	var $permissions = array(
		'add' => array('admin','manag'),
		'index' => array('admin','manag'),
		'view' => array('admin','manag','emplo','firms'),
		'edit' => array('admin','manag'),
		'delete' => array('admin','manag'),
		'register' => '*'
	);
	
	public $presetVars = array(
		array('field' => 'name', 'type' => 'like'),
		array('field' => 'surname', 'type' => 'like'),
		array('field' => 'address1', 'type' => 'like'),
		array('field' => 'address3', 'type' => 'like'),
		array('field' => 'address4', 'type' => 'like'),
		array('field' => 'address5', 'type' => 'like'),
		array('field' => 'phone1', 'type' => 'like')
	);
	
	
	function customer() {
		if($this->group == 'firms'){
			$id = $this->Customer->field('id',array('user_id'=>$this->Session->read("Auth.User.id")));
			$this->redirect(array('action'=>'view',$id));
		}else{
			$this->Session->setFlash(__("Invalid customer", true));
			$this->redirect(array('controller'=>'users','action'=>'login'));
		}
	}
		
	public function beforeFilter(){
		parent::beforeFilter();	
		$this->group = $this->Session->read("Auth.User.group");
	}
	
	function index() {
		$this->Customer->recursive = 0;
		$this->set('customers', $this->paginate());
	}
	
	public function find() {
		$this->Prg->commonProcess();
		$this->paginate['conditions'] = $this->Customer->parseCriteria($this->passedArgs);
		$this->set('customers', $this->paginate());
		
		$this->autoRender = false;
		$this->render('index');
	}


	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__("Invalid customer", true));
			$this->redirect(array('action' => 'index'));
		}
		// slowniki zdefiniowane w Orders
		App::import('Controller','orders'); 
		$this->set(OrdersController::dictionaries());
		
		$typeOfServices = $this->Customer->Order->TypeOfService->find('list');
		$typesOfSpaces = $this->Customer->Order->TypesOfSpaces->find('list');
		$this->set(compact('typeOfServices', 'typesOfSpaces'));
		
		$this->set('customer', $this->Customer->read(null, $id));
	}
	
	function register(){
		if (!empty($this->data)) {
			
			$this->Customer->set( $this->data );
			if ($this->Customer->validates($this->data)){
				$this->data['User']['email'] = $this->data['Customer']['email'];
				$this->data['User']['password'] = $this->Auth->password($this->data['Customer']['password']);	
				$this->data['User']['status'] = 1;
				$this->data['User']['group'] = 'firms';
									
				$this->User->create();
				if ($this->User->save($this->data)) {
					$this->data['Customer']['user_id'] = $this->User->getLastInsertID();
					$this->data['Customer']['registration_date'] = date('Y-m-d H:i:s');
					$this->Customer->create();
					if ($this->Customer->save($this->data)) {
						$this->Session->setFlash(__("The customer has been saved", true));
						$this->redirect(array('action' => 'index'));
					} else {
//						$this->Session->setFlash(__("The customer could not be saved. Please, try again.", true));
					}
				} else {
					//$this->Session->setFlash(__("The customer could not be saved. Please, try again.", true));
				}

			} else {
				$this->Session->setFlash(__("The customer could not be saved. Please, try again.", true), 'flash_error');	
				//$this->redirect($this->referer());
			}
		}

	}

	function add() {
		if (!empty($this->data)) {			
			
			$this->Customer->set( $this->data );
			if ($this->Customer->validates($this->data)){
				$this->data['User']['email'] = $this->data['Customer']['email'];
				$this->data['User']['password'] = $this->Auth->password($this->data['Customer']['password']);	
				$this->data['User']['status'] = 1;
				$this->data['User']['group'] = 'firms';
									
				$this->User->create();
				if ($this->User->save($this->data)) {
					$this->data['Customer']['user_id'] = $this->User->getLastInsertID();
					$this->data['Customer']['registration_date'] = date('Y-m-d H:i:s');
					$this->Customer->create();
					if ($this->Customer->save($this->data)) {
						$this->Session->setFlash(__("The customer has been saved", true));
						$this->redirect(array('action' => 'index'));
					} else {
//						$this->Session->setFlash(__("The customer could not be saved. Please, try again.", true));
					}
				} else {
					//$this->Session->setFlash(__("The customer could not be saved. Please, try again.", true));
				}

			} else {
				$this->Session->setFlash(__("The customer could not be saved. Please, try again.", true), 'flash_error');	
				//$this->redirect($this->referer());
			}
		}
		$users = $this->Customer->User->find('list');
		$this->set(compact('users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__("Invalid customer", true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Customer->save($this->data)) {
				$this->Session->setFlash(__("The customer has been saved", true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__("The customer could not be saved. Please, try again.", true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Customer->read(null, $id);
		}
		$users = $this->Customer->User->find('list');
		$this->set(compact('users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__("Invalid id for customer", true));
			$this->redirect(array('action'=>'index'));
		}
		$user_id = $this->Customer->read('user_id',$id);
		if ($this->Customer->delete($id)){
			$this->User->delete($user_id['Customer']['user_id']);
			$orders = $this->Customer->Order->find('list',array(
				'conditions'=>array('Order.customer_id'=>$id),
				'fields' => array('Order.id', 'Order.name')));
			if(is_array($orders)){
				foreach ($orders as $id => $name) {
					$this->Customer->Order->delete($id);
					$this->Customer->Order->OrdersCycle->deleteAll(array('OrdersCycle.orders_id'=>$id));
					$this->Customer->Order->OrdersBreak->deleteAll(array('OrdersBreak.orders_id'=>$id));
					$this->Customer->Order->OrdersEmployee->deleteAll(array('OrdersEmployee.orders_id'=>$id));
					$this->Customer->Order->OrdersSeries->deleteAll(array('OrdersSeries.orders_id'=>$id));	
					
				}
			}		
			$this->Session->setFlash(__("Customer deleted", true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__("Customer was not deleted", true));
		$this->redirect(array('action' => 'index'));
	}

}
?>